# coding=utf8
import glgui2
import hexfield
import glgui2
import GameLogic
import GameRender
import Map
import Mission

class guiPlanner:
  def show(self):
    #~ if not self.loaded:
      #~ self.map.load('test.map')
      #~ self.mission.load('test.mis')
      #~ self.loaded = True
    self.layout.visible = True
    glgui2.common_signals.set_block('events/mouse/mouse_press',self.on_mouse_press,False)
    self.hide_menu(None)
    
  def hide(self):
    
    glgui2.common_signals.set_block('events/mouse/mouse_press',self.on_mouse_press,True)
    self.layout.visible = False
    
  def show_menu(self,sender):
      self.menu.visible = True
  
  
  def hide_menu(self,sender):
      self.menu.visible = False
      
  def set_squad_list(self,squads):
    spr_list = []
    print 'call'
    for squad in squads:
      spr_list.append( self.mission.iconset.dictionary[squad.type] )
    self.fireteam_select.set_elements(spr_list)
    
  def on_mouse_press(self,x,y,buttons,modifiers):
    if self.layout.hit_test(x,y):
      return
      
    print buttons,modifiers
    if modifiers == 1 and buttons == 17:
      self.mission.add_top_squad_to_selection(x,y)
    elif buttons == 2 and modifiers == 16:
      self.mission.select_top_squad(x,y)
      self.set_squad_list( self.mission.get_squads(x,y))
    
    
    #~ if len(self.points) > 2:
      #~ self.points = []
      
    #~ self.points.append(self.map.hexfield.tile_pos(x,y))
    #~ if len(self.points) > 1:
      #~ self.path.set_points(self.map.get_quickest_path(type,self.points[0],self.points[-1]))

  def platoon_selected(self,index):
    print index
  
  def company_selected(self,index):
    self.platoon_select.set_elements([self.mission.iconset.dictionary['msv'],self.mission.iconset.dictionary['msv'], \
    self.mission.iconset.dictionary['msv'],self.mission.iconset.dictionary['mso'],self.mission.iconset.dictionary['mso'],\
    self.mission.iconset.dictionary['mso']])
    
    
  def __init__(self):
    self.layout = glgui2.Layout((1024,768))
    self.incoset = GameRender.Iconset()
    self.layout.add_widget(glgui2.Button('planner/show_menu',u'Меню',pos = (900,10)))
    glgui2.common_signals.connect_to_channel('gui_events/on_click/planner/show_menu',self.show_menu)
    self.loaded = False
    self.map = Map.Map('game')
    self.mission = Mission.Mission(self.map)
    self.Plan = Plan(self.mission,0)
    
    #Menu1
    self.menu_layout = glgui2.CentralLayout((320,160))
    self.menu = glgui2.Frame('planner/menu',layout = self.menu_layout,pos = (350,400),size = (320,160))
    self.menu.layout.add_widget(glgui2.Button('planner/menu/continue',u'Продолжить'))
    glgui2.common_signals.connect_to_channel('gui_events/on_click/planner/menu/continue',self.hide_menu)
    
    self.menu.layout.add_widget(glgui2.Button('planner/menu/quit',u'Выйти'))
    self.menu.visible = False
    self.layout.add_widget(self.menu)
    
    #Operational command frame
    
    self.fireteam_menu = glgui2.Frame('planner/fireteam',u'Отделение',pos = (50,30),size = (250,100),layout = glgui2.CentralLayout((250,100)))
    self.fireteam_select = glgui2.HListBox('planner/fireteam_selector',[],pos = (40,60),size= (180,32))
    
    self.fireteam_menu.layout.add_widget(self.fireteam_select)
    self.fireteam_menu.layout.add_widget(glgui2.Button('planner/fireteame_in_group',u'Обьеденить в группу'))
    self.fireteam_menu.layout.update_rect()
    
    #Tactical command frame
    
    self.platoon_menu = glgui2.Frame('planner/platoon',u'Взвод',pos = (320,30),size = (250,100),layout = glgui2.CentralLayout((250,100)))
    self.platoon_select = glgui2.HListBox('planner/platoon_selector',[],pos = (40,60),size= (180,32))
    glgui2.common_signals.connect_to_channel('gui_events/on_selection_changed/planner/platoon_selector',self.platoon_selected)
    self.platoon_menu.layout.add_widget(self.platoon_select)
    
    self.company_menu = glgui2.Frame('planner/company',u'Рота',pos = (570,30),size = (250,100),layout = glgui2.Layout((250,100)))
    
    self.company_select = glgui2.HListBox('planner/company_selector',[ self.mission.iconset.dictionary['company1'], self.mission.iconset.dictionary['company2'], self.mission.iconset.dictionary['company3']],pos = (40,60),size= (180,32))
    self.company_menu.layout.add_widget(self.company_select)
    
    glgui2.common_signals.connect_to_channel('gui_events/on_selection_changed/planner/company_selector',self.company_selected)
    
    self.layout.add_widget(self.platoon_menu)
    self.layout.add_widget(self.company_menu)
    self.layout.add_widget(self.fireteam_menu)
    self.layout.update_rect()
    
    self.path = hexfield.HexfieldPath(self.map.hexfield)
    self.map.hexfield.add_markers(self.path)
    self.points = []
    glgui2.common_signals.connect_to_channel('events/mouse/mouse_press',self.on_mouse_press)
    
  def draw(self):
    self.map.draw()
    self.layout.draw()


'''
Squad orders info:
 type - 'redeploy','assault','stand','support','ambush'
 route - should be provided by Navigation module
 target - if there is some specific targets like 'armored','infantry','structure','antiaircarft'
 alert_status - 'low','combat','high' regulates the stamina exhausting and caution 
'''

class SquadOrder:
  def __init__(self,squad,route):
    self.squad = squad
    self.type = type
    self.route = route
    self.target = target
    self.alert_status = ''
    
'''
 Group squad orders
  type - depends on group
'''

class GroupOrder:
  def __init__(self,squd_group):
    pass

class Commander:
  def __init__(self):
    pass

"""
Group 
@type - fireteam,platoon or company
@state - non-combat, pre-combat, combat
@formation - column, line, back angle, forward angle, left, right 
default formations: column for non-combat, line for precombat, forward angle for combat
"""

    
    
class Plan:
  def __init__(self,mission,timer):
    
    self.routing = None
    self.timer = timer
    self.orders = []
    self.groups = []
    self.platoon = {}
    
  def add_fireteam_to_platoon(self,fireteam,platoon):
    pass
  
  def add_fireteam_to_company(self,fireteam,platoon):
      pass
    
  def select_fireteam(self,fireteam_name):
    pass
  
  def select_platoon(self,patoon_name):
    pass
  
  def select_company(self,company_name):
    pass

  def add_order(self,squad):
    pass
  
  def remove_order():
    pass
    
  def set_draw_filter(self,filter):
    pass

